package com.wanrue.tcc.admin.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.wanrue.tcc.admin.entity.Transaction;
import com.wanrue.tcc.admin.etl.DataImport;
import com.wanrue.tcc.admin.etl.JdbcImport;
import com.wanrue.tcc.admin.etl.RedisImport;
import com.wanrue.tcc.admin.service.ITransactionService;
import com.wanrue.tcc.admin.util.Page;
import com.wanrue.tcc.admin.util.Result;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
public class TransactionController {

    @Resource
    private ITransactionService transactionService;

    private Set<DataImport> ds = new HashSet<>();

    @GetMapping("server")
    public Result server() {
        return Result.SUCCESS(transactionService.getServer());
    }

    @GetMapping("list")
    public Result list(Page page, String server, String type, String status, Integer count, Integer result) {
        Map map = new HashMap();
        map.put("type", type);
        map.put("server", server);
        map.put("status", status);
        map.put("count", count);
        map.put("result", result);
        page.setParam(map);
        transactionService.findPage(page);
        return Result.SUCCESS(page);
    }

    @PostMapping("import")
    public Result data(Integer type, String url, String user, String password, Integer port, String table, Integer db) {
        ds.clear();
        DataImport dataImport = null;
        if (type == 1) {
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSource.setUrl(url);
            dataSource.setUsername(user);
            dataSource.setPassword(password);
            dataSource.setBreakAfterAcquireFailure(true);
            dataImport = new JdbcImport("jdbc", table, dataSource);
            ds.add(dataImport);
        } else if (type == 2) {
            RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
            configuration.setHostName(url);
            configuration.setPassword(RedisPassword.of(password));
            configuration.setPort(port);
            configuration.setDatabase(db);
            LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(configuration);
            lettuceConnectionFactory.afterPropertiesSet();
            dataImport = new RedisImport("redis", lettuceConnectionFactory);
            ds.add(dataImport);
        }
        transactionService.setDataImport(dataImport);
        new Thread(() -> transactionService.updateCache()).start();
        return Result.SUCCESS();
    }

    @GetMapping("has")
    public Result has() {
        return Result.SUCCESS(ds.size());
    }

    @PostMapping("update")
    public Result update(Transaction transaction) {
        transaction.setUpdateTime(new Date());
        Transaction byId = transactionService.findById(transaction.getId());
        transaction.setVersion(byId.getVersion());
        int i = transactionService.update(transaction);
        return Result.SUCCESS(i);
    }

}
